using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SkillDefinition;
using SemanticKernelBlazorDemo.Core.Utils;

namespace SemanticKernelBlazorDemo.Core.Services;

public class WriterService : IWriterService {
    private readonly IKernel _kernel;
    private readonly ISKFunction _randomWrite;
    private readonly ILogger<WriterService> _logger;

    public WriterService(ILogger<IKernel> kernelLogger,
        IKernelProvider kernelProvider, ILogger<WriterService> logger) {
        _kernel = kernelProvider.GetKernel(kernelLogger);
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));

        _randomWrite =
            _kernel.ImportSemanticSkillFromDirectory(
                ProjectFile.GetSkillsFolder(), "WriterSkill")["TopicWrite"];
    }

    public async Task<string> TopicWriteAsync(string topic) {
        var context = new ContextVariables(topic);
        var result = await _kernel.RunAsync(context, _randomWrite);
        return result.Result;
    }
}